package com.uchiha.nova.mainService.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uchiha.nova.core.model.dto.NovaOauthTokenDTO;
import com.uchiha.nova.core.model.entity.OauthClientDetails;
import com.uchiha.nova.core.model.entity.OilUserEntity;
import com.uchiha.nova.core.model.entity.UserEntity;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.constant.RedisCacheConstants;
import com.uchiha.nova.core.tool.support.AuthToken;
import com.uchiha.nova.core.tool.support.NovaUser;
import com.uchiha.nova.core.tool.util.AuthUtil;
import com.uchiha.nova.core.util.AesEncryptUtil;
import com.uchiha.nova.mainService.service.IOilUserService;
import com.uchiha.nova.mainService.service.OauthTokenService;
import com.uchiha.nova.mainService.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @Author 周林静
 * @Date 2025-08-18 9:11
 * @Version 1.0
 */
@Service("oil_user")
public class OilUserTokenImpl implements OauthTokenService {

    @Resource
    UserService userService;
    @Resource
    PasswordEncoder passwordEncoder;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    IOilUserService iOilUserService;
    @Value("${nova.oauth.token-key}")
    String tokenKey;

    @Override
    public R getToken(NovaOauthTokenDTO dto) {
        String username = dto.getUsername();
        String password = AesEncryptUtil.desEncrypt(dto.getPassword());
        UserEntity userEntity = userService.getOne(new QueryWrapper<UserEntity>().eq("username",username).eq("tenant_id",dto.getTenantId()));
        if(ObjectUtil.isEmpty(userEntity)){
            return R.fail("无效的用户名");
        }
        boolean match = passwordEncoder.matches(password,userEntity.getPassword());
        if(!match){
            return R.fail("无效的密码");
        }
        OilUserEntity oilUserEntity = iOilUserService.getById(userEntity.getId());
        OauthClientDetails clientDetails = (OauthClientDetails) redisTemplate.opsForValue().get(RedisCacheConstants.NOVA_OAUTH_CLIENT+dto.getClientId());
        NovaUser user = userService.getNovaUser(userEntity);
        user.setClientId(dto.getClientId());
        String roleName = user.getRoleName();
        if(roleName.equals("ROLE_Oil_Province")){
            user.setDataAuth(oilUserEntity.getProvinceCode());
        }else if(roleName.equals("ROLE_Oil_City")){
            user.setDataAuth(oilUserEntity.getCityCode());
        }else if(roleName.equals("ROLE_Oil_Area")){
            user.setDataAuth(oilUserEntity.getAreaCode());
        }else{
            user.setDataAuth(oilUserEntity.getStreetCode());
        }

        AuthToken authToken = AuthUtil.getToken(user,tokenKey, (long) (1000*clientDetails.getAccessTokenValidity()), (long) (1000*clientDetails.getRefreshTokenValidity()));
        authToken.setUserId(userEntity.getId());
        return R.data(authToken);
    }
}
